// pages/giftstrategy/giftstrategy.js  strategy_list
var shihan = require('../../../common/fetch');
var config = require('../../../common/config');
var app = getApp()
var winScale = 750 / app.data.deviceInfo.windowWidth
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: 1,
    page_size: 10,
    set_map_id: 0,
    mapfilter: '',
    showLoading: true,
    sceneselect: 'search_arrow_down3',
    filterselect: 'search_arrow_down',
    scenetit: '送礼场景',
    sceneflag: true,
    filterflag: false,
    strategyflag: true,
    mapscurrentTab: 0,
    hasMore: true,
    filterConflag: false,
    strategytheme: [],
    filterarr: [],
    sceneselflag: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.strategylist()
  },
  strategylist: function() {
    var that = this;
    shihan.sendRequest(config.strategy_list + that.data.set_map_id + '/' + that.data.page + '-' + that.data.page_size + '.html', 'GET', {})
      .then(function(response) {
        // console.log(response)
        if (response.data.code == 0) {
          response.data.data.sh_set_data.unshift({
            sh_id: 0,
            sh_name: "全部",
            sh_set_map: []
          });
          // that.deletefilter(response.data.data.sh_set_data);
          if (response.data.data.sh_theme.length == 0) {
            that.setData({
              hasMore: false,
              strategyflag: false,
              showLoading: false
            })
          } else {
            that.setData({
              page: that.data.page + 1,
              strategytheme: that.data.strategytheme.concat(response.data.data.sh_theme),
              strategyflag: true,
              hasMore: true
            });
          }
          that.setData({
            strategylist: response.data.data,
            showLoading: false
          });
        } else {
          that.setData({
            hasMore: false,
            strategyflag: false,
            showLoading: false
          })
        }
      }, function(error) {
        console.log(error);
      });
  },
  refreshstrategylist: function() {
    var that = this;
    shihan.sendRequest(config.strategy_list + that.data.set_map_id + '/' + that.data.page + '-' + that.data.page_size + '.html', 'GET', {})
      .then(function(response) {
        // console.log(response)
        if (response.data.code == 0) {
          response.data.data.sh_set_data.unshift({
            sh_id: 0,
            sh_name: "全部",
            sh_set_map: []
          });
          that.deletefilter(response.data.data.sh_set_data);
          that.setData({
            page: that.data.page + 1,
            strategytheme: that.data.strategytheme.concat(response.data.data.sh_theme),
            showLoading: false
          });
        } else {
          that.setData({
            showLoading: false
          })
        }
      }, function(error) {
        console.log(error);
      });
  },
  mapsonNavbarTap: function(e) {
    this.deletefilter(this.data.strategylist.sh_set_data);
    this.setData({
      filterflag: false,
      strategylist: this.data.strategylist,
      mapscurrentTab: e.currentTarget.dataset.index
    });
    if (this.data.mapscurrentTab > 0) {
      this.setData({
        sceneselflag: true,
        filtermapflag: true,
        filterselect: 'search_arrow_down3',
        scenetit: this.data.strategylist.sh_set_data[this.data.mapscurrentTab].sh_name
      })
    } else {
      this.setData({
        scenetit: '送礼场景',
        sceneselflag: false
      })
    }
    this.otherclosefilter();
  },
  selectscene: function() { //选择场景
    if (this.data.mapscurrentTab > 0) {
      if (this.data.filterConflag == false) {
        this.setData({
          sceneflag: true,
          filterConflag: true,
          sceneselect: 'search_arrow_up_'
        })
      } else {
        this.setData({
          sceneflag: false,
          filterConflag: false,
          sceneselect: 'search_arrow_down_'
        })
        // this.otherclosefilter();
      }
    } else {
      if (this.data.filterConflag == false) {
        this.setData({
          sceneflag: true,
          filterConflag: true,
          sceneselect: 'search_arrow_down3_'
        })
      } else {
        this.setData({
          sceneflag: false,
          filterConflag: false,
          sceneselect: 'search_arrow_down3'
        })
      }
    }
  },
  selectobject: function() { //选择对象
    this.selectarr();
    if (this.data.mapscurrentTab > 0) {

      if (this.data.filtermapflag) {
        this.setData({
          filterselect: 'search_arrow_down3_'
        })
      } else {
        this.setData({
          filterselect: 'search_arrow_up'
        })
      }
      this.setData({
        sceneflag: false,
        filterConflag: true,
      })
      if (this.data.set_map_id) {
        this.setData({
          filterselect: 'search_arrow_up_'
        })
      }
    }
  },
  setmapTap: function(e) {
    // var selectflag = 0;
    var sceneindex = this.data.mapscurrentTab;
    var filterindex = e.currentTarget.dataset.index;
    var scenelist = this.data.strategylist.sh_set_data;
    if (scenelist[sceneindex].sh_set_map[filterindex].select == 0) {
      // this.setData({
      //   filterflag: true
      // })
      scenelist[sceneindex].sh_set_map[filterindex].select = 1
    } else {
      scenelist[sceneindex].sh_set_map[filterindex].select = 0
    }
    // for (var i = 0; i < scenelist[sceneindex].sh_set_map.length;i++){
    //   if (scenelist[sceneindex].sh_set_map[i].select == 1){
    //     var selectflag = 1;
    //   }
    // }
    // if (selectflag == 0){
    //   this.setData({
    //     filterflag: false
    //   })
    // }
    this.setData({
      strategylist: this.data.strategylist
    })
  },
  deletefilter: function(scenelist) {
    for (var i = 0; i < scenelist.length; i++) {
      for (var j = 0; j < scenelist[i].sh_set_map.length; j++) {
        scenelist[i].sh_set_map[j].select = 0;
      }
    }
  },
  otherclosefilter: function() {
    this.setData({
      sceneflag: false,
      filterConflag: false,
      sceneselect: 'search_arrow_down_'
    })
    if (this.data.mapscurrentTab > 0) {
      if (this.data.set_map_id) {
        this.setData({
          filterflag: true,
          filterselect: 'search_arrow_down_'
        })
      } else {
        if (this.data.filtermapflag) {
          this.setData({
            // filterflag: false,
            filterselect: 'search_arrow_down3'
          })
        } else {
          this.setData({
            // filterflag: false,
            filterselect: 'search_arrow_down'
          })
        }
      }
    } else {
      // this.deletefilter(this.data.strategylist.sh_set_data);
      this.setData({
        page: 1,
        set_map_id: 0,
        mapfilter: '',
        filterflag: false,
        sceneselflag: false,
        strategytheme: [],
        filterarr: [],
        filtermapflag: false,
        sceneselect: 'search_arrow_down3',
        // strategylist: this.data.strategylist,
        filterselect: 'search_arrow_down'
      })
      if (this.data.strategytheme.length == 0) {
        this.refreshstrategylist()
      }
    }
  },
  sceneconfirm: function() {
    var arr = [];
    var filterarr = [];
    var sceneindex = this.data.mapscurrentTab;
    var scenelist = this.data.strategylist.sh_set_data;
    for (var i = 0; i < scenelist[sceneindex].sh_set_map.length; i++) {
      if (scenelist[sceneindex].sh_set_map[i].select == 1) {
        arr.push(scenelist[sceneindex].sh_set_map[i].sh_id);
        filterarr.push(scenelist[sceneindex].sh_set_map[i].sh_name);
      }
    }
    if (arr.length > 0) {
      this.setData({
        filterarr: arr,
        set_map_id: arr.join(','),
        mapfilter: filterarr.join(','),
        filterselect: 'search_arrow_down_'
      })
    } else {
      this.setData({
        set_map_id: 0,
        mapfilter: '',
        filterarr: arr,
        filterflag: false
      })
    }
    this.setData({
      page: 1,
      strategytheme: []
    })
    if (this.data.strategytheme.length == 0) {
      this.refreshstrategylist()
    }
    this.otherclosefilter();
  },
  selectarr: function() {
    var sceneindex = this.data.mapscurrentTab;
    var scenelist = this.data.strategylist.sh_set_data;
    for (var i = 0; i < scenelist[sceneindex].sh_set_map.length; i++) {
      for (var j = 0; j < this.data.filterarr.length; j++) {
        if (scenelist[sceneindex].sh_set_map[i].sh_id == this.data.filterarr[j]) {
          scenelist[sceneindex].sh_set_map[i].select = 1
        }
      }
    }
    this.setData({
      strategylist: this.data.strategylist
    })
  },
  scenereset: function() {
    var sceneindex = this.data.mapscurrentTab;
    var scenelist = this.data.strategylist.sh_set_data;
    for (var i = 0; i < scenelist[sceneindex].sh_set_map.length; i++) {
      scenelist[sceneindex].sh_set_map[i].select = 0
    }
    this.setData({
      filterflag: false,
      filterarr: [],
      set_map_id: 0,
      mapfilter: '',
      filterselect: 'search_arrow_up',
      strategylist: this.data.strategylist
    })
  },
  openstrategyinfo: function(e) {
    if (e.currentTarget.dataset.url) {
      var advertname = e.currentTarget.dataset.name || ''
      wx.navigateTo({
        url: '/pages/strategyinfo/strategyinfo?id=' + e.currentTarget.dataset.id + '&url=' + encodeURIComponent(e.currentTarget.dataset.url) + '&name=' + advertname
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    if (this.data.strategyflag) {
      this.setData({
        strategyflag: false
      });
      this.strategylist();
    }
  }
})